package com.project.communitymanagement.common;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @ProjectName: hotelNew
 * @Package: com.setup.mysql.common
 * @ClassName: BaseServiceImpl
 * @Author: "XGP"
 * @Description:
 * @Date: 2021/09/13 11:29
 * @Version: 1.0
 */
public abstract class BaseServiceImpl<T extends BaseMapper,Model extends BaseEntity> implements BaseService<Model> {
    protected abstract T mapper();
    protected abstract Model defaultModel();
    @Override
    public Page<Model> getPages(Page<Model> page) {
        if(page.getRequestData().getCreate_user() != null && page.getRequestData().getCreate_user().equals("admin")){
            page.getRequestData().setCreate_user(null);
        }
        if(page.getRequestData()==null)
            page.setRequestData(defaultModel());
        List<Model> lst =mapper().getPage(page);
        int count = mapper().getTotal(page);
        return Page.getPage(lst,count);
    }
    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public int save(Model model) {
        if(StringUtils.hasText(model.getId())){
            return mapper().updateByPrimaryKeySelective(model);
        }
        model.saveInit();
        model.setCreate_date(new Date());
        return mapper().insert(model);
    }
    @Override
    public int edit(Model model){
        return mapper().updateByPrimaryKeySelective(model);
    }
    @Override
    public int del(Integer Id){
        return mapper().deleteByPrimaryKey(Id);
    }
}

